package cn.itcast.controler;

import cn.itcast.CheckGroupService;
import cn.itcast.constant.MessageConstant;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.PageResult;
import cn.itcast.pojo.QueryPageBean;
import cn.itcast.pojo.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    CheckGroupService checkGroupService;



    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);
        } catch (Exception e) {
            //新增失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //新增成功
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;


    }

    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") long CheckGroupId) {
        CheckGroup group;
        try {
            group = checkGroupService.findById(CheckGroupId);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, group);

    }

    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public List<Integer> CheckItemIdsByCheckGroupId(@Param("id") Integer id) {
        List<Integer> CheckItemId = checkGroupService.findCheckItemIdsByCheckGroupId(id);

        return CheckItemId;

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,@RequestParam("checkitemIds") Integer[] checkItemIds) {
        try {
            checkGroupService.edit(checkGroup, checkItemIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);

    }
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){
        try {
            checkGroupService.delete(id);

        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());

        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);

    }
    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckGroup>  groupList=checkGroupService.findAll();


            if (groupList!=null&&groupList.size()>0) {
                return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, groupList);

            }
           return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);


    }



}
